//  Date:         08/08/2018
//  task:         education analysis
//  project:      World Development

capture log close
clear all
macro drop _all
set more off
version 14
set matsize 800

use actionable_all.dta, clear
drop _m

tabulate primarytopic, gen(topic)
replace year=year+2000

gen modate = ym(year, month) 
format modate %tm 

gen Any=1

* All relevant messages
preserve
keep modate topic1 topic2 topic4 topic7 Any
collapse (sum) Community=topic1 Education=topic2 Health=topic4 Water=topic7 Any=Any, by(modate)
drop if modate==.

lab var Any "Any relevant message"
lab var Community "Community development messages"
lab var Education "Education messages"
lab var Health "Health messages"
lab var Water "Water messages"

# delimit ;		
tw lowess Education modate, lp(dash) lw(thick) || 
   lowess Health modate, lp(shortdash_dot) lw(thick) ||
   lowess Water modate, lw(thick) xtitle("") ytitle("N. Messages") name(topic1) title(Relevant messages)
   legend(pos(7) ring(0) col(4) lab(1 "Education") lab(2 "Health") lab(3 "Water") stack);
   #delimit cr	
restore 
 
* only actionable messages
preserve

keep if action_narrow==1
keep modate topic1 topic2 topic4 topic7 Any
collapse (sum) Community=topic1 Education=topic2 Health=topic4 Water=topic7 Any=Any, by(modate)
drop if modate==.

lab var Any "Any relevant message"
lab var Community "Community development messages"
lab var Education "Education messages"
lab var Health "Health messages"
lab var Water "Water messages"

# delimit ;		
tw lowess Education modate, lp(dash) lw(thick) || 
   lowess Health modate, lp(shortdash_dot) lw(thick) ||
   lowess Water modate, lw(thick) xtitle("") ytitle("N. Messages") name(topic2) title(Actionable messages)
   legend(pos(7) ring(0) col(4) lab(1 "Education") lab(2 "Health") lab(3 "Water") stack);
   #delimit cr	
restore

*********************************************
*FIGURE 5 in paper
*********************************************

* graph actionable and relevant messages by core topic (sector)
  graph combine topic1 topic2, title("Messages overtime, by type") ycommon	

* total messaging	
keep modate Any action_narrow
collapse (sum) Action=action_narrow Any=Any, by(modate)
drop if modate==.
	
	gen Any_Cum = Any if _n == 1
	replace Any_Cum = Any_Cum[_n-1] + Any if _n > 1 
	
	gen Action_Cum = Action if _n == 1
	replace Action_Cum = Action_Cum[_n-1] + Action if _n > 1 

# delimit ;	
tw lowess Any_Cum modate, lp(dash) lw(thick) || 
   lowess Action_Cum modate, lp() lw(thick)	 xtitle("") ytitle("N. Messages") name(cumulative) title(Cumulative) ylabel(0(500)3000)
   legend(pos(10) ring(0) col(2) lab(1 "Relevant") lab(2 "Actionable") stack);
   #delimit cr		
 
# delimit ;	
tw lowess Any modate, lp(dash) lw(thick) || 
   lowess Action modate, lp() lw(thick)	 xtitle("") ytitle("N. Messages") name(monthly) title(Monthly) legend(off);
   #delimit cr

*********************************************
*FIGURE 2 in paper
*********************************************
   
  graph combine monthly cumulative, title("Message Intesnity Overtime") 